package com.zjzywl.smoke.controller;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;
import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjzywl.smoke.repository.DeviceRepository;
import com.zjzywl.smoke.repository.OrgDevRepository;
import com.zjzywl.smoke.repository.OrganizationRepository;
import com.zjzywl.smoke.util.WebResult;
import com.zjzywl.smoke.entity.Device;
import com.zjzywl.smoke.entity.OrgDev;
import com.zjzywl.smoke.entity.Organization;

@RestController
@RequestMapping(value = "/orgdev")
public class OrgDevController extends BaseController {

	@Autowired
	private OrgDevRepository orgdevRepository;
	@Autowired
	private OrganizationRepository organizationRepository;
	@Autowired
	private DeviceRepository deviceRepository;

	@RequestMapping(value = "/data/list")

	public WebResult getOrgDevData() {
		try {
			List<OrgDev> orgdev = orgdevRepository.findAll();
			return WebResult.OK(orgdev);
		} catch (Exception e) {
			return WebResult.FAIL(e.getMessage());
		}
	}

	/*
	 * 仅返回组织下面直接挂靠的设备数量
	 */
	@RequestMapping(value = "/count/{orgid}")

	public WebResult countDevicesByOrg(@PathVariable("orgid") Integer orgid) {
		try {
			List<Integer> count = orgdevRepository.countDevicesByOrg(orgid);
			return WebResult.OK(count.get(0));
		} catch (Exception e) {
			return WebResult.FAIL(e.getMessage());
		}
	}
	@RequestMapping(value = "/list/{orgid}")
	public List<OrgDev> findByOrgid(@PathVariable("orgid") Integer orgid) {
		List<OrgDev> orgdevs = orgdevRepository.findByOrgid(orgid);
		return orgdevs;
	}
	
	@RequestMapping(value = "/findAll")
	public List<Device> findAll() {
		List<Device> findAll = deviceRepository.findAll();
		return findAll;
	}
	@RequestMapping(value = "/findDeviceByaddressLoc/{address}")
	public List<Device> findDeviceByaddressLoc(@PathVariable("address") String address) {
		List<Device> findAll = deviceRepository.findDeviceByaddressLoc(address);
		return findAll;
	}
	
	@RequestMapping(value = "/findAllOrgs")
	public List<Organization> findAllOrgs() {
		List<Organization> orgs= organizationRepository.findAll();
		return orgs;
	}
	/*
	 * 保存组织设备对应关系
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)

	public WebResult save(@Valid List<OrgDev> orgdevs) {
		try {
			Integer count = orgdevs.size();
			List<Integer> number = orgdevRepository.countDevicesByOrg(orgdevs.get(0).getOrganization().getId());
			count += number.get(0);
			List<OrgDev> oldOrgDevs = orgdevRepository.findByOrgid(orgdevs.get(0).getOrganization().getId());
			for (OrgDev orgdev : oldOrgDevs) {
				orgdevs.add(orgdev);
			}
			for (OrgDev orgdev : orgdevs) {
				orgdev.setNumber(count);
				orgdevRepository.save(orgdev);
			}
			return WebResult.OK(orgdevs);
		} catch (Exception e) {
			return WebResult.FAIL(e.getMessage());
		}
	}

	/*
	 * 修改组织挂靠关系
	 */
	@PostMapping(value = "/modify")

	public WebResult modify(@PathParam("orgid") Integer orgid, @PathParam("arr") Integer[] arr) {
		try {
			List<OrgDev> orgdevs = new ArrayList<OrgDev>();
			orgdevRepository.deleteByOrg(orgid);
			if (arr != null) {
				Organization org = organizationRepository.findById(orgid);
				int count = arr.length;
				for (int i = 0; i < arr.length; i++) {
					Device dev = deviceRepository.findOne(arr[i]);
					OrgDev orgdev = new OrgDev();
					orgdev.setOrganization(org);
					orgdev.setNumber(count);
					orgdev.setDevice(dev);
					orgdevs.add(orgdev);
				}
				orgdevRepository.save(orgdevs);
			}
			return WebResult.OK(orgdevs);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return WebResult.FAIL(e.getMessage());
		}
	}

	/*
	 * 统计当前用户设备类型数量
	 */
	@GetMapping(value = "/devicetype/count")

	public WebResult deviceTypeCountByUser() {
		String userId = this.getUserId();
		List devCounts = orgdevRepository.deviceTypeCountByUser(Integer.valueOf(userId));
		return WebResult.OK(devCounts);
	}

}
